package cn.xshi.common.util;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * @Desc GetClientIp
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Slf4j
public class GetClientIp {

    /**
     * 获取客户端IP
     * @param request
     * @return
     */
	public String getIpAddr(HttpServletRequest request) {
//		String ip = request.getHeader("x-forwarded-for"); //apache
		String ip = request.getHeader("X-Real-IP");//nginx
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip.split(",")[0];
	}
	
	
	public static String getIpAddrW(HttpServletRequest request) {   
        String ip = request.getHeader("X-Forwarded-For");   
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {   
            ip = request.getHeader("Proxy-Client-IP");   
        }   
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {   
            ip = request.getHeader("WL-Proxy-Client-IP");   
        }   
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {   
            ip = request.getHeader("HTTP_CLIENT_IP");   
        }   
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {   
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");   
        }   
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {   
            ip = request.getRemoteAddr();   
        }   
        return ip;   
    }

    /**
     * 获取本机IP
     * @return
     */
    public static String getLocalIp(){
	    try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            return inetAddress.getHostAddress();
        }catch (Exception e){
	        log.error("获取本机IP异常：{}",e);
	        return "unknown";
        }
    }

    /**
     * 获取本机名
     * @return
     */
    public static String getLocalName(){
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            return inetAddress.getHostName();
        }catch (Exception e){
            log.error("获取本机名异常：{}",e);
            return null;
        }
    }

    /**
     * 查询本机物理机以太网所有ip
     * @return
     */
    public static List<String> getLocalEthernetIpList(){
        List<String> localIpList = new ArrayList<>();
        Enumeration<NetworkInterface> interfaces = null;
        try {
            interfaces = NetworkInterface.getNetworkInterfaces();//获取本机的所有网络接口
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress address = addresses.nextElement();
                    //判断该地址是否为回环地址（Loopback Address），即localhost ，同时判断并且为本地地址（Site Local Address）
                    if (!address.isLoopbackAddress() && address.isSiteLocalAddress()) {
                        localIpList.add(address.getHostAddress());
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return localIpList;
    }
    /**
     * 查询本机所有ip
     * @return
     */
    public static List<String> getLocalIpList(){
        List<String> localIpList = new ArrayList<>();
        Enumeration<NetworkInterface> interfaces = null;
        try {
            interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress address = addresses.nextElement();
                    System.out.println("isAnyLocalAddress--"+address.isAnyLocalAddress());
                    System.out.println("isLinkLocalAddress--"+address.isLinkLocalAddress());
                    System.out.println("isSiteLocalAddress--"+address.isSiteLocalAddress());
                    System.out.println("isMCLinkLocal--"+address.isMCLinkLocal());
                    System.out.println("isMCNodeLocal--"+address.isMCNodeLocal());
                    System.out.println("isMCGlobal--"+address.isMCGlobal());
                    System.out.println("isMCOrgLocal--"+address.isMCOrgLocal());
                    System.out.println("isMCSiteLocal--"+address.isMCSiteLocal());
                    System.out.println("isMulticastAddress--"+address.isMulticastAddress());
                    System.out.println("isMCSiteLocal--"+address.getCanonicalHostName());
                    System.out.println("getHostAddress--"+address.getHostAddress());
                    if (!address.isLoopbackAddress()) {
                        localIpList.add(address.getHostAddress());
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return localIpList;
    }
}
